<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- <div v-show="this.$route.query.mode !== 'ONE'" v-if="crud.props.searchToggle"> -->
        <!-- 搜索 -->
        <el-input v-model="crud.queryInput.value" clearable placeholder="输入搜索内容" style="width: 200px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <el-select v-model="crud.queryInput.type" clearable placeholder="类型" class="filter-item" style="width: 130px">
          <el-option v-for="item in queryTypeOptions" :key="item.key" :label="item.display_name" :value="item.key" />
        </el-select>
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="组织名称" prop="teamName">
            <el-input v-model="form.teamName" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="组织机构职责">
            <el-input v-model="form.specialCase" :rows="3" type="textarea" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="创建时间" prop="startTime">
            <el-date-picker v-model="form.startTime" type="datetime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="撤销时间">
            <el-date-picker v-model="form.endTime" type="datetime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="描述">
            <el-input v-model="form.describe" :rows="3" type="textarea" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column v-if="columns.visible('id')" prop="id" label="ID" />
        <el-table-column v-if="columns.visible('teamName')" prop="teamName" label="组织名称" />
        <el-table-column v-if="columns.visible('specialCase')" prop="specialCase" label="组织机构职责" />
        <el-table-column v-if="columns.visible('startTime')" prop="startTime" label="创建时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.startTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('endTime')" prop="endTime" label="撤销时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.endTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('describe')" prop="describe" label="描述" />
        <el-table-column v-permission="['admin','teamBase:edit','teamBase:del']" fixed="right" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudTeamBase from '@/api/teamBase'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

// crud交由presenter持有
const defaultCrud = CRUD({ title: '组织基础信息', url: 'api/teamBase', sort: 'id,asc', crudMethod: { ...crudTeamBase }})
const defaultForm = { id: null, teamName: null, specialCase: null, startTime: null, endTime: null, describe: null }
export default {
  name: 'TeamBase',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  data() {
    return {
      permission: {
        add: ['admin', 'teamBase:add'],
        edit: ['admin', 'teamBase:edit'],
        del: ['admin', 'teamBase:del']
      },
      rules: {
        teamName: [
          { required: true, message: '必须填写该项', trigger: 'blur' }
        ],
        startTime: [
          { required: true, message: '必须填写该项', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'id', display_name: '组织ID' },
        { key: 'teamName', display_name: '组织名称' },
        { key: 'describe', display_name: '描述' }
      ]
    }
  },
  created() {
    // 初始化搜索条件
    this.crud.query.type = null
    this.crud.queryInput.value = null
    this.crud.query = {}
    // 设置默认搜索条件
    if (this.$route.query.mode === 'ONE') {
      this.crud.queryInput.type = 'id'
      this.crud.queryInput.value = this.$route.query.id
    }
    // 注意这里需要手动refreash加载数据，如果crud自动加载数据，则无法保证设置query发生在refreah之前
    this.crud.refresh()
  },
  methods: {
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      // 叠加queryInput中的搜索条件
      if (this.crud.queryInput.type && this.crud.queryInput.value) {
        this.crud.query[this.queryInput.type] = this.queryInput.value
      }
      return true
    }
  }
}
</script>

<style scoped>

</style>
